package org.example.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.bean.Category;
import org.example.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/category")
@Tag(name = "商品分类管理",description = "实现对商品分类的增删改查工作")
public class CategoryController {
    public static final int CATEGORY_STATUS_ACTIVE= 1;
    public static final int CATEGORY_STATUS_INACTIVE= 0;
    @Autowired
    private CategoryService categoryService;
    @PostMapping("/add")
    @Operation(summary = "添加分类",description = "添加分类信息")
    public ResponseResult addCategory(@RequestParam("category_name") String category_name,
                                      @RequestParam("link_url") String link_url,
                                      @RequestPart("icoImgFile") MultipartFile icoImgFile){//文件操作使用@RequestPart
        Category category = new Category();
        category.setCategory_name(category_name);
        category.setIcoImgFile(icoImgFile);
        category.setLink_url(link_url);
        category.setCategory_status(CATEGORY_STATUS_ACTIVE);
        category=categoryService.addCategory(category);  //返回值是添加后的category
        category.setIcoImgFile(null);
        return ResponseResult.builder().code(1).msg("添加成功").data(category).build();
    }

    //根据状态查询分类信息
    //PathVariable  传参 参数值不能为null
    @GetMapping("/list/{categoryStatus}")
    @Operation(summary = "根据状态查询分类信息",description = "根据状态查询分类信息")
    public ResponseEntity<ResponseResult> getCategories(@PathVariable("categoryStatus") Integer categoryStatus){
        List<Category> categories = categoryService.getCategories(categoryStatus);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(categories).build());
    }



    //根据id查询分类信息
    @GetMapping("/getCategoryById/{categoryId}")
    @Operation(summary = "根据id查询分类信息",description = "根据id查询分类信息")
    public ResponseEntity<ResponseResult> getCategoryById(@PathVariable("categoryId") Integer categoryId){
        Category category = categoryService.getCategoryById(categoryId);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(category).build());
    }

    //根据id修改类别的状态
    @PutMapping("/updateCategoryStatus/{categoryId}/{category_status}")
    @Operation(summary = "根据id修改类别的状态",description = "根据id修改类别的状态")
    public ResponseEntity<ResponseResult> updateCategoryStatus(@PathVariable("categoryId") Integer categoryId,
                                                               @PathVariable("category_status") Integer category_status){
//        boolean result = categoryService.updateCategoryStatus(categoryId, category_status);
        Category category = categoryService.updateCategoryStatus(categoryId, category_status);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("修改成功").data(category).build());
    }
//    更新分类信息
    @PutMapping("/updateCategory/{category_id}")
    @Operation(summary = "更新分类信息",description = "更新分类信息")
    public ResponseEntity<ResponseResult> updateCategory(@PathVariable("category_id") Integer category_id,
                                                         @RequestParam("category_name") String category_name,
                                                         @RequestParam("link_url") String link_url,
                                                         @RequestPart("icoImgFile") MultipartFile icoImgFile){
        Category category = new Category();
        category.setCategory_id(category_id);
        category.setCategory_name(category_name);
        category.setLink_url(link_url);
        category.setIcoImgFile(icoImgFile);
        category=categoryService.updateCategory(category);
        category.setIcoImgFile(null);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("修改成功").data(category).build());
    }

}
